import {Injectable} from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class SelectedService {

  bedroom = [];

  constructor() {
  }

  findOne(tar: any, arr: []): boolean {
    return arr.find(val => {
      return Object.is(tar, val);
    });
  }

  selected(type: string, multiply: boolean, payload: any): void {
    const target = this[type];
    if (multiply) {
      if (this.findOne(payload, target)) {
        target.splice(target.findIndex(val => {
          return Object.is(val, payload);
        }), 1);
      } else {
        target.push(payload);
      }
    } else {
      this[type] = [payload];
    }
  }

}
